package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.Setmeal;
import com.itheima.service.RoleService;
import com.itheima.utils.QiniuUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author HHF
 * @date 2020/12/24 - 17:08
 * @describe 角色管理控制层
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询角色
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 添加角色
     * @param permissions
     * @param menus
     * @param role
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result add(Integer[] permissions,Integer[] menus, @RequestBody Role role){
        try {
            roleService.add(permissions,menus,role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 根据id查询角色
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer roleId){
        Role role = roleService.findById(roleId);
        if(role !=null){
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }
        else{
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 根据角色id查询角色菜单中间表记录(二级菜单)
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/findMenusByRoleId",method = RequestMethod.GET)
    public Integer[] findMenusByRoleId(Integer roleId){
        return roleService.findMenusByRoleId(roleId);
    }

    /**
     * 根据角色id查询角色权限中间表记录
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/findPermissionsByRoleId",method = RequestMethod.GET)
    public Integer[] findPermissionsByRoleId(Integer roleId){
        return roleService.findPermissionsByRoleId(roleId);
    }

    /**
     * 编辑角色
     * @param permissions
     * @param menus
     * @param role
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public Result edit(Integer[] permissions,Integer[] menus, @RequestBody Role role){
        try {
            roleService.edit(permissions,menus,role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    /**
     * 删除角色
     * @param roleId
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(Integer roleId){
        try {
            roleService.deleteById(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 角色名查重
     * @param roleName
     */
    @RequestMapping(value = "/checkRoleName",method = RequestMethod.GET)
    public Result checkRoleName(String roleName){
        try {
            Role role = roleService.findByName(roleName);
            if (role != null){
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS);
            }else {
                return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 角色关键字查重
     * @param roleKeyWord
     */
    @RequestMapping(value = "/checkRoleKeyWord",method = RequestMethod.GET)
    public Result checkRoleKeyWord(String roleKeyWord){
        try {
            Role role = roleService.findByKeyWord(roleKeyWord);
            if (role != null){
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS);
            }else {
                return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

}
